	
/******************************************************************************
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:				Description:
**		--------		--------				-------------------------------------------
**    
*******************************************************************************/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[a_Articles_selListNewer]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[a_Articles_selListNewer]
GO

CREATE PROCEDURE [dbo].a_Articles_selListNewer
	@OrderBy varchar(50),
	@OrderDirection varchar(5),
	@Page int,
	@PageSize int,
	@SubCat int,
	@IdItem int
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

CREATE TABLE #Tempa_Articles (
	RowNumber INT IDENTITY (1, 1) NOT NULL,
	ID int	
)

DECLARE @sql nvarchar(2000)
DECLARE @Top int

SET @Top = @Page*@PageSize
IF @PageSize > 0
   SET ROWCOUNT @Top
-- insert primary keys into temp table
SET @sql =	N'INSERT INTO #Tempa_Articles ([ID]) SELECT '
SET @sql = @sql + ' [ID] FROM [dbo].[a_Articles] WHERE  [SubCatID]=' + CAST(@SubCat AS VARCHAR(10)) +  N' AND [dbo].[a_Articles].[ID]>' + CAST(@IdItem AS VARCHAR(10)) + N' ORDER BY [' + @OrderBy + N'] ' + @OrderDirection
EXEC (@sql)
SET ROWCOUNT 0

--SELECT @TotalRecords = COUNT(*) FROM [a_Articles]

SELECT
	[dbo].[a_Articles].[ID],
	[UTitle],
	[KTitle],
	[UAuthor],
	[UBody],
	[KBody],
	[MainCatID],
	[SubCatID],
	[CountryID],
	[SeasonID],
	[FYear],
	[PostDate],
	[PostBy_UserID],
	[PublishDate],
	[PublishStatus],
	[StatPopularity],
	[StatRating],
	[Permission],
	[IsFeatured],
	[FeaturedType],
	[LayoutFormat],
	[UniqueKeywords],
	[IsEdited],
	[Brand],
	[GroupID]
FROM
	#Tempa_Articles AS tblTemp JOIN [dbo].[a_Articles] ON
	tblTemp.ID = [dbo].[a_Articles].ID 	
WHERE ((@PageSize = 0) OR (@PageSize > 0 AND (@Page - 1)*@PageSize < RowNumber AND RowNumber <= @Page*@PageSize)) AND [SubCatID]=@SubCat AND [dbo].[a_Articles].[ID]>@IdItem
ORDER BY RowNumber

DROP TABLE #Tempa_Articles;


/*******************************************************************************
**		Date:		Author:				Description:
**		--------		--------				-------------------------------------------
**    
*******************************************************************************/


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[a_Articles_selListLaster]
	@OrderBy varchar(50),
	@OrderDirection varchar(5),
	@Page int,
	@PageSize int,
	@SubCat int,
	@IdItem int
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

CREATE TABLE #Tempa_Articles (
	RowNumber INT IDENTITY (1, 1) NOT NULL,
	ID int	
)

DECLARE @sql nvarchar(2000)
DECLARE @Top int

SET @Top = @Page*@PageSize
IF @PageSize > 0
   SET ROWCOUNT @Top
-- insert primary keys into temp table
SET @sql =	N'INSERT INTO #Tempa_Articles ([ID]) SELECT '
SET @sql = @sql + ' [ID] FROM [dbo].[a_Articles] WHERE  [SubCatID]=' + CAST(@SubCat AS VARCHAR(10)) +  N' AND [dbo].[a_Articles].[ID]<' + CAST(@IdItem AS VARCHAR(10)) + N' ORDER BY [' + @OrderBy + N'] ' + @OrderDirection
EXEC (@sql)
SET ROWCOUNT 0

--SELECT @TotalRecords = COUNT(*) FROM [a_Articles]

SELECT
	[dbo].[a_Articles].[ID],
	[UTitle],
	[KTitle],
	[UAuthor],
	[UBody],
	[KBody],
	[MainCatID],
	[SubCatID],
	[CountryID],
	[SeasonID],
	[FYear],
	[PostDate],
	[PostBy_UserID],
	[PublishDate],
	[PublishStatus],
	[StatPopularity],
	[StatRating],
	[Permission],
	[IsFeatured],
	[FeaturedType],
	[LayoutFormat],
	[UniqueKeywords],
	[IsEdited],
	[Brand],
	[GroupID]
FROM
	#Tempa_Articles AS tblTemp JOIN [dbo].[a_Articles] ON
	tblTemp.ID = [dbo].[a_Articles].ID 	
WHERE ((@PageSize = 0) OR (@PageSize > 0 AND (@Page - 1)*@PageSize < RowNumber AND RowNumber <= @Page*@PageSize)) AND [SubCatID]=@SubCat AND [dbo].[a_Articles].[ID]<@IdItem
ORDER BY RowNumber

DROP TABLE #Tempa_Articles;


